<template>
	<view class="recordListbg">
		<view class="pad30" v-if="isLogin">
			<view class="cards" v-for="(it,i) in dataList" :key="i">
				<view class="flex-c">
					<view class="fontsize32 fontbold colorMain">保顺物流</view>
					<!-- <view class="colorMain paiche fontsize24 marL20">{{it.coalType}}</view> -->
					<view class="paiche bgRed colorfff fontsize24 marL20">{{it.coalType}}</view>
				</view>
				<view class="flex-cb marT20">
					<view class="color999">出发地:</view>
					<view>{{it.origin}}</view>
				</view>
				<view class="flex-cb marT20">
					<view class="color999">目的地:</view>
					<view>{{it.destination}}</view>
				</view>
				<view class="flex-cb marT20">
					<view class="color999">公里数:</view>
					<view>{{it.distanceKm}}KM</view>
				</view>
				<view class="flex-cb marT20">
					<view class="color999">需求车辆数:</view>
					<view>{{it.requiredTrucks}}</view>
				</view>
				<view class="flex-cb marT20">
					<view class="color999">已报名人数:</view>
					<view>{{it.applyCount}}</view>
				</view>
				<view class="flex-cb marT20">
					<view class="color999">运费:</view>
					<view class="colorRed">￥{{it.freightFee}}</view>
				</view>
				<view class="flex-cb marT20">
					<view class="color999">发布时间:</view>
					<view>{{it.createTime}}</view>
				</view>
				<view class="flex-cb marT20">
					<view class="color999">派车时间:</view>
					<view>{{it.dispatchTime}}</view>
				</view>
				<view class="line"></view>
				<view class="flex-cb marT20">
					<view class="flex-c">
						<view>{{it.contactName}}</view>
						<view class="marL10">{{it.contactPhone}}</view>
					</view>
					<view v-if="it.currentUserApplied" class="jiedan bg999">已报名</view>
					<view v-else-if="it.status===0" class="jiedan bg999">报名截止</view>
					<view v-else class="jiedan bgMain" @click="clickItem(it)">点击报名</view>
				</view>
			</view>
			<view style="margin: 40rpx 0;">
				<u-loadmore :status="status" />
			</view>
		</view>
		<view v-else class="pad30">
			<u-loadmore status="nomore" nomoreText='登录后解锁更多服务~' />
		</view>

		<view @touchmove.stop.prevent="()=>{return false}">
			<u-modal :show="modalShow" :showCancelButton="true" @cancel="modalShow=false;" @confirm="apply">
				<view class="slot-content">
					<u--form labelPosition="left" labelWidth="100">
						<u-form-item label="车队长" prop="captainUserName" borderBottom @click="pickerShow=true">
							<u--input v-model="captainUserName" disabled disabledColor="#ffffff" placeholder="请选择车队长"
								border="none"></u--input>
							<u-icon slot="right" name="arrow-right"></u-icon>
						</u-form-item>
						<u-form-item label="金额" prop="freightFee" borderBottom>
							<view>{{data.freightFee}}</view>
						</u-form-item>
					</u--form>
				</view>
			</u-modal>
		</view>
		<view @touchmove.stop.prevent="()=>{return false}">
			<u-picker :show="pickerShow" :columns="columns" keyName="nickname" @cancel="pickerShow=false;"
				@confirm="pickerConfirm"></u-picker>
		</view>

	</view>
</template>

<script>
	import commonApi from '@/api/common.js'
	export default {
		data() {
			return {
				page: 1,
				size: 10,
				status: 'loading',
				dataList: [],
				total: 0,
				modalShow: false,
				captainUserName: '',
				captainUserId: '',
				data: {},
				pickerShow: false,
				columns: [
					[]
				],
			}
		},
		onShow() {
			if (this.isLogin) {
				switch (this.plateType) {
					case 0:
						this.page = 1;
						this.status = 'loading';
						this.dataList = [];
						this.freightPlanList()
						break;
					default:
						this.status = 'nomore';
						this.dataList = [];
						break;
				}
			}
		},
		methods: {
			freightPlanList() {
				commonApi.freightPlanList({
					page: this.page,
					size: this.size,
				}).then((res) => {
					if (res.status == 200) {
						let resOrderList = res.data.records;
						this.dataList = this.dataList.concat(resOrderList);
						this.total = res.data.total;
						if (resOrderList.length < this.size) {
							this.status = "nomore";
						}
					}
				});
			},
			clickItem(item) {
				this.data = item;
				if (item.currentUserIsCaptain) {
					this.captainUserId = this.userInfo.id;
					this.apply()
				} else {
					this.captainUserName = '';
					this.captainUserId = '';
					this.columns[0] = this.data.captainUserList;
					this.modalShow = true;
				}
			},
			pickerConfirm(e) {
				this.captainUserName = e.value[0].nickname;
				this.captainUserId = e.value[0].captainUserId;
				this.pickerShow = false;
			},
			apply() {
				if(!this.captainUserId){
					this.showMsg('请选择车队长')
					return
				}
				commonApi.apply(this.data.id, {
					captainUserId: this.captainUserId,
					planId: this.data.id
				}).then((res) => {
					if (res.status == 200) {
						this.modalShow = false;
						this.page = 1;
						this.status = 'loading';
						this.dataList = [];
						this.freightPlanList()
					}
				});
			},
		},

		onReachBottom() {
			if (this.status == 'nomore') {
				return;
			}
			this.page++;
			this.freightPlanList()
		},
	}
</script>

<style lang="scss" scoped>
	.recordListbg {
		width: 100%;
		min-height: 100%;

		.cards {
			padding: 20rpx;
			margin-bottom: 20rpx;
			border-radius: 20rpx;
			background: #fff;

			.paiche {
				border-radius: 50rpx;
				padding: 3rpx 15rpx;
				text-align: center;
			}

			.line {
				width: 100%;
				height: 1rpx;
				background-color: #eee;
				margin: 20rpx 0;
			}

			.jiedan {
				border-radius: 50rpx;
				text-align: center;
				width: 200rpx;
				height: 80rpx;
				line-height: 80rpx;
				color: #fff;
				font-size: 30rpx;
			}
		}
	}
</style>
